package top.anydata.lifeofcode.lifesystem.dao.app.user;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import top.anydata.lifeofcode.lifesystem.entity.app.user.AppUserEntity;

import java.util.List;

/**
 * 功能描述:
 *
 * @author <a href="mailto:caingao@anydata.top">CainGao</a>
 * @version V_1.0
 * @date 2024/5/8 3:11 PM
 */
@Repository
public interface IAppUserRepository extends JpaRepository<AppUserEntity,Integer> {

    @Query(value = "SELECT *, ( 6371 * acos ( cos ( radians(:latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:longitude) ) + sin ( radians(:latitude) ) * sin( radians( latitude ) ) ) ) AS distance FROM t_app_user HAVING distance < :radius ORDER BY distance LIMIT :pageSize OFFSET :offset", nativeQuery = true)
    List<AppUserEntity> findUsersNearby(
            @Param("latitude") double latitude,
            @Param("longitude") double longitude,
            @Param("radius") double radius,
            @Param("pageSize") int pageSize,
            @Param("offset") int offset
    );

}
